<?php

class Default_IndexController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction() {
		if ( Zend_Auth::getInstance()->hasIdentity() ) {
			Zend_Auth::getInstance()->clearIdentity();
			$this->unsetCookie();
		} 
		$form = new Application_Form_Login();
		$form->appendValidationEngineFiles($this->view);
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				if ($this->_process($form->getValues())) {
					$auth = Zend_Auth::getInstance()->getIdentity();
					$roleId = $auth->role_id;
					switch($roleId) {
						case 1:
							$this->_helper->redirector('index','index','admin');
							break;
						case 2:
							$this->_helper->redirector('index','index','technical');
							break;
						case 3:
							$this->_helper->redirector('index','index','lecturer');
							break;
						default:
							$this->_helper->redirector('index','index','default');
					}
					//
				}
			}
		}
		$this->view->form = $form;
		//print_r($_COOKIE);

	}

	protected function _process($values)
	{
		// Get our authentication adapter and check credentials
		$adapter = $this->_getAuthAdapter();
		$adapter->setIdentity($values['email']);
		$adapter->setCredential($values['password']);
		$adapter->setCredentialTreatment('SHA1(CONCAT(?,hashcode))');
		$emailModel = new Application_Model_Dao_User();
		$hashcode = $emailModel->getHashcode($values['email']);

		if ( $hashcode == null) {
			return false;
		} else {

			$pass = SHA1($values['password'].$hashcode);
			$auth = Zend_Auth::getInstance();
			$result = $auth->authenticate($adapter);
			if ($result->isValid()) {
				if($values['remember']==1){

					$this->setCookie($values['email'],$pass);
				}

				$user = $adapter->getResultRowObject();
				$auth->getStorage()->write($user);
				$dbAdapter = Zend_Db_Table::getDefaultAdapter();
				//$select = $dbAdapter->select()->from('users') ->where('users.email = \''.$values['email'].'\'')->joinLeft(array('roles'), 'users.role_id = roles.id', array('designation'));
				$auth->getStorage()->write($user);


				$idRole = $user->role_id;
				$roleObj = new Application_Model_Dao_Role();
				$designation = $roleObj->getRole($idRole);
				$namespace = new Zend_Session_Namespace('Zend_Auth');
				$namespace->storage->designation = $designation;

				return true;
			} else {
				return false;
			}
		}

	}

	protected function _getAuthAdapter() {
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter,'users','email','password');
		return $authAdapter;
	}

	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		// print_r($_COOKIE);
		$this->unsetCookie();
		// print_r($_COOKIE);

		$this->_helper->redirector('index','index', 'default'); // back to login page
	}

	private function setCookie($login, $password)
	{
		setcookie('log',$login, time()+3600*2, '/');
		setcookie('pas',$password, time()+ 3600*2, '/');
	}

	private function unsetCookie()
	{
		setcookie('log','', time()-3600, '/');
		setcookie('pas','', time()-3600, '/');
	}

	public function resetpasswordAction()

	{
		$form = new Application_Form_Email();
		$form->appendValidationEngineFiles($this->view);
		$this->view->form = $form;
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$emailModel = new Application_Models_Dao_Users();
				$pass = $this->resetPass();
				$email = $form->getValue('email');
				$hashcode = $emailModel->getHashcode($email);
				$newPass = SHA1($pass.$hashset);
				//$sendMail = $this->sendMail($emial, $newPass);
				//if($sendMail){

					$emailModel->updatePassword($email, $newPass);

					$this->_helper->redirector('index','index','default');
					//}else{
					// echo "Nie wyslano emaila";
					//}
					//}

				}


			}
		}
		private function resetPass($length = 11){
			$newPass = md5(time());
			$newPass = substr($newPass,0,$length);
			return($newPass);
		}
		private function sendMail($email, $password){
			$mail = new Zend_Mail();
			$mail->setBodyText('Login: '.$email.'
Haslo: '.$password);
			$mail->setFrom('somebody@example.com', 'lukasz kokosza');
			$mail->addTo($email);
			$mail->setSubject('Nowe Haslo');
			//sprawdzic czy zwroci co zwroci?:)
			return $mail->send();
		}
		private function hashPass($pass, $hashcode){
			return SHA1($pass.$hashcode);
		}
	}







